package com.cool4j.generator;

import com.cool4j.generator.bean.DatabaseBean;
import com.cool4j.generator.bean.TableBean;
import com.cool4j.generator.db.DBInfoHelper;
import com.cool4j.generator.util.PropUtil;
import com.cool4j.generator.util.VelocityUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成类
 *
 * @author Frank create on 2016/1/6 17:25
 */
public class CodeGenerator {

    // 是否去掉模块前面的编号
    public static String flag = "0";

    /** 基础包名 */
    private String classPackage = "com.cool4j";
    /** 作者 */
    private String author = "Frank";
    /** 数据库配置 */
    private String driver = "com.mysql.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/test";
    private String username = "root";
    private String password = "123456";
    /** 代码生成路径 */
    private String codePath = "E:/codegen/source/";
    private String sourcePath = codePath + "src/main/java/";
    private String testPath = codePath + "src/test/java/";
    private String jspPath = codePath + "src/main/webapp/";

    public void config(String classPackage, String author, String codePath, String driver,
            String url, String username, String password) {
        this.classPackage = classPackage;
        this.author = author;
        this.codePath = codePath;
        this.driver = driver;
        this.url = url;
        this.username = username;
        this.password = password;

        sourcePath = codePath + "src/main/java/";
        testPath = codePath + "src/test/java/";
        jspPath = codePath + "src/main/webapp/";
    }

    public boolean generator(String tableNamePattern) {
        /** 数据库 */
        DatabaseBean databaseBean = null;

        /** 数据库表 */
        List<TableBean> tableList = null;

        try {
            databaseBean = new DBInfoHelper(driver, url, username, password).tableInfo(tableNamePattern);
            tableList = databaseBean.getTableList();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        /** 遍历生成代码 */
        for (TableBean table : tableList) {
            table.setDomainPackage(this.classPackage);
            Map<String, Object> map = new HashMap<>();
            map.put("table", table);
            map.put("author", author);

            System.out.println("生成表: " + table.getName());
            // 生成实体类
            VelocityUtil.generatorCode("DomainTemplate.vm", map, this.sourcePath + table.getDomainPath() + "/db/domain", table.getDomainClassName() + ".java");
            // 生成实体类对应 xml 文件
            VelocityUtil.generatorCode("DomainMapperXmlTemplate.vm", map, this.sourcePath + table.getDomainPath() + "/db/mapper", table.getDomainClassName() + "Mapper.xml");
            // 生成 Mapper 类
            VelocityUtil.generatorCode("DomainMapperTemplate.vm", map, this.sourcePath + table.getDomainPath() + "/db/mapper", table.getDomainClassName() + "Mapper.java");
            // 生成 Service 类
            VelocityUtil.generatorCode("ServiceTemplate.vm", map, this.sourcePath + table.getDomainPath() + "/service", table.getDomainClassName() + "Service.java");
            // 生成 Controller 类
            VelocityUtil.generatorCode("ControllerTemplate.vm", map, this.sourcePath + table.getDomainPath() + "/controller", table.getDomainClassName() + "Controller.java");
            // 生成单元测试类
            VelocityUtil.generatorCode("UnitTestTemplate.vm", map, this.testPath + table.getDomainPath() + "/service/test", table.getDomainClassName() + "ServiceTest.java");
            // 生成 jsp 列表显示页面
            VelocityUtil.generatorCode("JqueryJspListTemplate.vm", map, this.jspPath + "jsp", table.lowerDomainClassName() + ".jsp");
            // 生成 jsp 修改页面
            VelocityUtil.generatorCode("JspEditTemplate.vm", map, this.jspPath + "jsp", table.lowerDomainClassName() + "_edit.jsp");
            // Extjs 列表页面
            VelocityUtil.generatorCode("JqueryTemplate.vm", map, this.jspPath + "jsp/js", table.lowerDomainClassName() + ".js");
        }

        System.out.println("**************** 代码生成完成 ****************");
        System.out.println("代码路径: " + this.codePath);
        System.out.println("包: " + this.classPackage);
        System.out.println("作者: " + this.author);
        System.out.println("********************************************");

        return true;
    }

    /**
     * Execute Method
     *
     * @param args -
     * @throws SQLException - SQL 异常
     */
    public static void main(String[] args) throws SQLException {

        // CodeGenerator dg = new CodeGenerator(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
        CodeGenerator codeGenerator = new CodeGenerator();

        String propertyFilePath = "config.properties";

        String classPackage = PropUtil.getPropertyFromFile(propertyFilePath, "classPackage");
        String author = PropUtil.getPropertyFromFile(propertyFilePath, "author");
        String codePath = PropUtil.getPropertyFromFile(propertyFilePath, "codePath");
        String classDriver = PropUtil.getPropertyFromFile(propertyFilePath, "driver");
        String url = PropUtil.getPropertyFromFile(propertyFilePath, "url");
        String username = PropUtil.getPropertyFromFile(propertyFilePath, "username");
        String password = PropUtil.getPropertyFromFile(propertyFilePath, "password");
        String tableNamePattern = PropUtil.getPropertyFromFile(propertyFilePath, "tableNamePattern");

        codeGenerator.config(classPackage, author, codePath, classDriver, url, username, password);
        codeGenerator.generator(tableNamePattern);

    }


}
